package cn.tedu.controller;

import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("stu")
public class StudentController {
    @RequestMapping("add")
    public Student get(Student stu) throws Exception {
        Class<?> d = Class.forName("com.mysql.cj.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/cgb2106";
        Connection c = DriverManager.getConnection(url, "root", "root");

        String sql="insert into tb_student(name,age,sex,hobby,edu,date) values (?,?,?,?,?,?)";
        PreparedStatement p = c.prepareStatement(sql);
        p.setString(1,stu.getName());
        p.setInt(2,stu.getAge());
        p.setString(3,stu.getSex());
        p.setObject(4, Arrays.toString(stu.getHobby()));
        p.setString(5,stu.getEdu());
        p.setObject(6,stu.getDate());

        int i = p.executeUpdate(); //增删改操作

        p.close();
        c.close();



        return stu;
    }
}
